﻿
@{
    ViewBag.Title = "新闻列表";
}
@section scripts{
    <script type="text/javascript">
        var $tbGrid = {};
        $(function() {
            $.globalParas.datagrid.url = '@Url.Action("GetNewsPage", "News", new {area = "Admin"})';
            $.globalParas.datagrid.columns = [
                [
                    { field: 'Title', title: '标题', width: 100, align: 'center', formatter: dgvFormat },
                    { field: 'Keyword', title: '关键字', width: 60, align: 'center', formatter: dgvFormat },
                    { field: 'Channels', title: '所属栏目', width: 140, align: 'center', formatter: dgvFormat },
                    { field: 'IsTop', title: '顶置', width: 30, align: 'center', formatter: dgvFormat },
                    { field: 'IsMoveImg', title: '首页轮播', width: 40, align: 'center', formatter: dgvFormat },
                    { field: 'Click', title: '点击数', width: 30, align: 'center', formatter: dgvFormat },
                    { field: 'UserName', title: '发布人', width: 40, align: 'center', formatter: dgvFormat },
                    { field: 'CreateTime', title: '发布时间', width: 60, align: 'center', formatter: dgvFormat }
                ]
            ];
            $.globalParas.datagrid.idField = 'Id';
            $.globalParas.datagrid.loadFilter = function(response) {
                if (response.Statu === 1) {
                    return response.Data;
                } else
                    $.msgBoxObj.showMsgErr(response.Msg, true);
            };
            // 为'新增'按钮添加事件绑定
            $.globalParas.datagrid.bindBtnEvent('新增',
                function() {
                    $.globalParas.showComWindowObj({
                        title: '新增新闻',
                        width: 800,
                        height: 700,
                        url: '@Url.Action("AddNews", "News", new {area = "Admin"})',
                        callObject: $('#tbGrid')
                    });
                });
            // 为'修改'按钮添加事件绑定
            $.globalParas.datagrid.bindBtnEvent('修改',
                function() {
                    // 获取选定的数据
                    var rowData = $tbGrid.datagrid('getSelected');
                    // 判断是否选定了数据
                    if (rowData) {
                        $.globalParas.showComWindowObj({
                            title: '编辑新闻',
                            width: 800,
                            height: 700,
                            url: '@Url.Action("EditNews", "News", new {area = "Admin"})' + '?Id=' + rowData.Id,
                            callObject: $('#tbGrid')
                        });
                    } else
                        window.top.$.msgBoxObj.showMsgErr('未选定任何行!');
                });
            // 添加删除按钮
            $.globalParas.datagrid.bindBtnEvent('删除',
                function() {
                    // 获取选定的数据
                    var rowData = $tbGrid.datagrid('getSelected');
                    // 判断是否选定了数据
                    if (rowData) {
                        // 用户重复确认是否删除
                        window.top.$.messager.confirm('确认',
                            '您确认要删除记录吗？删除后无法恢复！',
                            function(isOk) {
                                if (isOk) {
                                    // post请求DeleUser Action
                                    $.ajax({
                                        url: '@Url.Action("DeleteNews", "News", new {area = "Admin"})',
                                        type: 'Delete',
                                        data: { Id: rowData.Id },
                                        error: function() {
                                            // 提示网络错误
                                            $.msgBoxObj.showMsgErr('网络错误!');
                                        },
                                        success: function(response) {
                                            // 根据返回状态码 执行对应提示
                                            if (response.Statu === 1) {
                                                $.msgBoxObj.showMsgOk(response.Msg);
                                                $tbGrid.datagrid('reload');
                                            } else {
                                                $.msgBoxObj.showMsgErr(response.Msg);
                                            }
                                            return;
                                        }
                                    });
                                }
                            });
                    } else
                        window.top.$.msgBoxObj.showMsgErr('未选定任何行!', true);
                });
            $.globalParas.datagrid.addBtn('置顶/取消置顶', function() {
                // 获取选定的数据
                var rowData = $tbGrid.datagrid('getSelected');
                // 判断是否选定了数据
                if (rowData) {
                    $.ajax({
                        url: '@Url.Action("PutNews", "News", new {area = "Admin"})',
                        type: 'put',
                        data: { Id: rowData.Id, IsTop: rowData.IsTop === true ? false : true },
                        error: function() {
                            // 提示网络错误
                            $.msgBoxObj.showMsgErr('网络错误!');
                        },
                        success: function(response) {
                            // 根据返回状态码 执行对应提示
                            if (response.Statu === 1) {
                                $.msgBoxObj.showMsgOk(response.Msg);
                                $tbGrid.datagrid('reload');
                            } else {
                                $.msgBoxObj.showMsgErr(response.Msg);
                            }
                            return;
                        }
                    });
                } else
                    window.top.$.msgBoxObj.showMsgErr('未选定任何行!', true);
            }, 'icon-filter');
            $.globalParas.datagrid.addBtn('轮播/取消轮播', function() {
                // 获取选定的数据
                var rowData = $tbGrid.datagrid('getSelected');
                // 判断是否选定了数据
                if (rowData) {
                    $.ajax({
                        url: '@Url.Action("PutNews", "News", new {area = "Admin"})',
                        type: 'put',
                        data: { Id: rowData.Id, IsMoveImg: rowData.IsMoveImg === true ? false : true },
                        error: function() {
                            // 提示网络错误
                            $.msgBoxObj.showMsgErr('网络错误!');
                        },
                        success: function(response) {
                            // 根据返回状态码 执行对应提示
                            if (response.Statu === 1) {
                                $.msgBoxObj.showMsgOk(response.Msg);
                                $tbGrid.datagrid('reload');
                            } else {
                                $.msgBoxObj.showMsgErr(response.Msg);
                            }
                            return;
                        }
                    });
                } else
                    window.top.$.msgBoxObj.showMsgErr('未选定任何行!', true);
            },'icon-large-clipart');
            // 初始化Grid控件
            $tbGrid = $("#tbGrid").datagrid($.globalParas.datagrid);
        });
    </script>
}
<table id="tbGrid"></table>